<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>数字输入框</title>
</head>
<body>
	<span style="font-size:18px;">
		<input type="text" onKeypress="handleKeyPress(event)" onblur="toHaveComma(event)" onfocus="this.value=this.value.replace(/,/gi,'')" />
	</span>
	<script>
		function toHaveComma(event) {  
		  let origin = event.target.value,
		  		fOrigin = +origin,
		  		val = Math.floor(fOrigin) + '',
		  		decimals = origin.substring(val.length),
		  		length = val.length;

		  for (let i = 1; i < length / 3; i++) {  
		      let temp = val.slice(0, -4 * i + 1);    //从后往前数，来获取前面的字符串  
		      val = val.replace(temp, temp + ',');    //给前面的字符串后面加一个逗号  
		  }
		  event.target.value = val + decimals;  
		}
		function handleKeyPress(event){
			return (/[\d\.]/.test(String.fromCharCode(event.keyCode)))
		}
	</script>
</body>
</html>